/* 
 * File:   Logger.cpp
 * Author: nahuel
 * 
 * Created on June 21, 2013, 7:57 PM
 */
#include "common_Logger.h"

#include <log4cpp/Appender.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/Layout.hh>
#include <log4cpp/BasicLayout.hh>
#include <log4cpp/Priority.hh>
#include <log4cpp/LoggingEvent.hh>
#include <time.h>


log4cpp::Category* Logger::_category;

class CustomLayout : public log4cpp::Layout {
    std::string format(const log4cpp::LoggingEvent& event) {        
        time_t seconds;
        time(&seconds);        
        
        char* time = asctime(gmtime((const time_t*)&seconds));
        std::string timeStamp(time);
        timeStamp = timeStamp.substr(0, timeStamp.size() - 1);
        
        return std::string("[") + timeStamp + std::string("] ") + event.message + std::string("\n");
    }
};

void Logger::Init(const std::string& fileName) {
    log4cpp::Appender* appender = new log4cpp::FileAppender("appender", fileName);
    appender->setLayout(new CustomLayout());
    
    log4cpp::Category& category = log4cpp::Category::getInstance(std::string("category"));
    category.setPriority(log4cpp::Priority::DEBUG);
    category.addAppender(appender);
    Logger::_category = &category;
}

void Logger::LogMessage(const std::string& msg) {
    Logger::_category->debug(msg);
}

void Logger::Shutdown() {
    Logger::_category->shutdown();
}